from multiprocessing import Process, Lock
from threading import Thread, Lock

#进程锁
pro_lock = Lock()
def pro_foo(index):
    with pro_lock:
        with open("test.txt", "a", encoding="utf-8") as f:
            f.write(str(index) + "\n")
#线程锁
thr_lock = Lock()
index = 0
def thr_foo():
    global index
    for i in range(10**5):
        with thr_lock:
            index += 1
            index -= 1

if __name__ == "__main__":
    pro_array = []
    thr_array = []
    for i in range(10):
        #p = Process(target=pro_foo, args=(i, ))
        t = Thread(target=thr_foo)
        #pro_array.append(p)
        thr_array.append(t)
        #p.start()
        t.start()
    #for p in pro_array:
        #p.join()
    for t in thr_array:
        t.join()
    print(index)
    print("done!!!")